<?php
/**
 * SCRIPT_NAME: AdminRule.php
 * Created by PhpStorm.
 * Time: 2016/3/18 21:10
 * FUNCTION:
 * @author: Doogie <461960962@qq.com>
 */

namespace app\admin\controller;

class AdminRule extends Base{

    private static $rule_m;

    public function __construct() {
        parent::__construct();
        self::$rule_m = D('AdminRule');
    }

    public function index(){
        //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['id|title'] = array(
            $condition,
            $condition,
            '_multi'=>true
        );
        $list = self::$rule_m->page(I('get.'.C('var_page')), C('admin_page_rows'))->where($map)->order('sort asc')->select();
        $page = $this->page('AdminRule', $map);

        $tree = new \app\common\library\Tree();
        $list = $tree->toFormatTree($list);
        //dump($list);exit;
        $this->assign([
            'list' => $list,
            'page' => $page->show(),
            'keyword' => $keyword
        ]);
        return $this->display();
    }

    /**
     * 添加
     * @return mixed|string
     */
    public function add(){
        if(IS_POST){
            $return_data = ['code' => 1, 'url' => '', 'msg' => '添加成功'];
            $_POST['url'] = strtolower($_POST['url']);
            $result = self::$rule_m->validate([
                // name和age是需要验证的字段名
                'name'=> ['require','标识不符合规范'],
                'title' => ['require','标题不符合规范'],
            ])->auto([
                // status和create_time是需要填充的字段名
                'status'=> 1, //
                'create_time' => NOW_TIME,
            ])->create();

            if(!$result){
                // 验证失败 返回错误信息
                $return_data['msg'] = self::$rule_m->getError();
                $return_data['code'] = 0;
            }else{
                self::$rule_m->add();
            }
            echo json_encode($return_data);exit;
        }else{
            $this->assign([
                'meta_title' => '新增菜单权限',
                'post_url' => url('add'),
                'type_list' => self::$rule_m->types(),
                'rule_tree' => select_list_as_tree('AdminRule', ['status'=>1], '顶部菜单')
            ]);

            return $this->display();
        }
    }

    /**
     * 修改
     * @return mixed|string
     */
    public function edit($id){
        if(IS_POST){
            $return_data = ['code' => 1, 'url' => '', 'msg' => '修改成功'];
            $_POST['url'] = strtolower($_POST['url']);
            $result = self::$rule_m->validate([
                // name和age是需要验证的字段名
                'name'=> ['require','标识不符合规范'],
                'title' => ['require','标题不符合规范'],
            ])->auto([
                'update_time' => NOW_TIME,
            ])->create();

            if(!$result){
                // 验证失败 返回错误信息
                $return_data['msg'] = self::$rule_m->getError();
                $return_data['code'] = 0;
            }else{
                self::$rule_m->save();
            }
            echo json_encode($return_data);exit;
        }else{
            $this->assign([
                'info' => self::$rule_m->find($id),
                'type_list' => self::$rule_m->types(),
                'meta_title' => '修改配置',
                'post_url' => url('edit'),
                'rule_tree' => select_list_as_tree('AdminRule', ['status'=>1], '顶部菜单')
            ]);

            return $this->display('add');
        }
    }
}